import { scanReceiveBadge } from "../api/map/map.js";
export default {
  methods: {
    getQueryString(url) {
      // 通过 ? 分割获取后面的参数字符串
      let urlStr = url.split("?")[1];
      // 创建空对象存储参数
      let obj = {};
      // 再通过 & 将每一个参数单独分割出来
      let paramsArr = urlStr.split("&");
      for (let i = 0, len = paramsArr.length; i < len; i++) {
        // 再通过 = 将每一个参数分割为 key:value 的形式
        let arr = paramsArr[i].split("=");
        obj[arr[0]] = arr[1];
      }
      return obj;
    },
    set_scanCode() {
      let that = this;
      uni.scanCode({
        // scanType:['datamatrix'],
        success(res) {
          console.log("1:" + decodeURIComponent(res.rawData));
          console.log("条码类型：" + JSON.stringify(res));
          console.log("条码内容：" + res);
          if (res.path) {
            let userInfo = uni.getStorageSync("userInfo") || {};
            let obj = that.getQueryString(res.path);

            let data = {
              pointId: obj.id,
              userId: userInfo.userId,
            };
            scanReceiveBadge(data).then((res) => {
              if (res.code === "0") {
                uni.showToast({
                  title: "领取成功",
                  icon: "success",
                  duration: 1500,
                });
              } else {
                uni.showToast({
                  title: res.message,
                  icon: "error",
                });
              }
            });
          }
        },
        fail(srr) {
          console.log("ss", srr);
        },
      });
    },
  },
};
